package com.saguitech.sgemeos.ui.activities;

import java.util.Calendar;
import java.util.GregorianCalendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageView;

import com.saguitech.sgemeos.R;
import com.saguitech.sgemeos.business.LoginBusiness;

public class ApplicationActivity extends Activity implements OnClickListener {

	ImageView all_expositionsButton;
	ImageView profileButton;
	ImageView my_expositionsButton;
	ImageView current_expositionsButton;
	ImageView expositorsButton;
	ImageView dailyExpositionButton;
	Button mainButton;
	Intent it;
	LoginBusiness login;

	private int mYear;
	private int mMonth;
	private int mDay;
	static final int DATE_DIALOG_ID = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.application);

		all_expositionsButton = (ImageView) findViewById(R.id.buttonAllExpositions);
		all_expositionsButton.setOnClickListener(this);

		profileButton = (ImageView) findViewById(R.id.buttonProfile);
		profileButton.setOnClickListener(this);

		my_expositionsButton = (ImageView) findViewById(R.id.buttonMyExpositions);
		my_expositionsButton.setOnClickListener(this);

		current_expositionsButton = (ImageView) findViewById(R.id.buttonCurrentExpositions);
		current_expositionsButton.setOnClickListener(this);

		expositorsButton = (ImageView) findViewById(R.id.buttonExpositors);
		expositorsButton.setOnClickListener(this);

		dailyExpositionButton = (ImageView) findViewById(R.id.buttonDailyExpositions);
		dailyExpositionButton.setOnClickListener(this);

		Button mainButton = (Button) findViewById(R.id.btnapp);
		Typeface font = Typeface.createFromAsset(getAssets(),
				"fonts/GOODTIME.ttf");
		mainButton.setTypeface(font);
		mainButton.setClickable(false);

		login = (LoginBusiness) getIntent().getSerializableExtra("login");

		Button refreshButton = (Button) findViewById(R.id.refreshButton);
		refreshButton.setVisibility(View.GONE);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.buttonAllExpositions:
			it = new Intent(ApplicationActivity.this, ScheduleActivity.class);
			it.putExtra("login", login);
			startActivity(it);
			break;
		case R.id.buttonProfile:
			it = new Intent(ApplicationActivity.this, ProfileActivity.class);
			it.putExtra("login", login);
			startActivity(it);
			break;
		case R.id.buttonMyExpositions:
			it = new Intent(ApplicationActivity.this,
					MyExpositionsActivity.class);
			it.putExtra("login", login);
			startActivity(it);
			break;
		case R.id.buttonCurrentExpositions:
			it = new Intent(ApplicationActivity.this, CurrentExpositionsActivity.class);
			it.putExtra("login", login);
			startActivity(it);
			break;
		case R.id.buttonExpositors:
			it = new Intent(ApplicationActivity.this,
					ExpositorListActivity.class);
			it.putExtra("login", login);
			startActivity(it);
			break;
		case R.id.buttonDailyExpositions:
			final Calendar c = Calendar.getInstance();
			mYear = c.get(Calendar.YEAR);
			mMonth = c.get(Calendar.MONTH);
			mDay = c.get(Calendar.DAY_OF_MONTH);
			
			showDialog(DATE_DIALOG_ID);
			break;
		}
	}

	public void onMenuButtonClick(View view) {
		it = new Intent(ApplicationActivity.this, ApplicationActivity.class);
		it.putExtra("login", login);
		it.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(it);
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DATE_DIALOG_ID:
			return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
					mDay);
		}
		return null;
	}

	private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			mYear = year;
			mMonth = monthOfYear;
			mDay = dayOfMonth;
			GregorianCalendar gc = new GregorianCalendar(mYear, mMonth,
					mDay);

			it = new Intent(ApplicationActivity.this,
					DailyExpositionsActivity.class);
			it.putExtra("login", login);
			it.putExtra("date", gc.getTime());
			startActivity(it);
		}
	};

}
